﻿using Sidvall.UI.Dialogs;
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Sidvall.UI.Uwp.Dialogs
{
    public sealed partial class MessageDialog
    {
        #region Public Members

        #region InitializeContext

        public void InitializeContext(MessageContext message)
        {
            if (message == null)
                throw new System.ArgumentNullException(nameof(message));

            this.Message = message;
            this.MessageText.Text = this.Message.Message;
            if (this.Message.MessageOptions.Count == 0)
                this.Message.MessageOptions.AddOk(true, true);
            this.ButtonPanel.Children.Clear();
            switch (this.Message.MessageOptions.Count)
            {
                case 1:
                    var option1 = this.Message.MessageOptions[0];
                    this.PrimaryButtonText = option1.Message;
                    break;
                case 2:
                    var option2 = this.Message.MessageOptions[0];
                    this.PrimaryButtonText = option2.Message;
                    option2 = this.Message.MessageOptions[1];
                    this.SecondaryButtonText = option2.Message;
                    break;
                default:
                    foreach (var messageOption in this.Message.MessageOptions)
                    {
                        var button = new Button();
                        button.Content = messageOption.Message;
                        button.Padding = new Thickness(15, 3, 15, 3);
                        button.MinWidth = 70;
                        if (this.ButtonPanel.Children.Count > 0)
                            button.Margin = new Thickness(8, 0, 0, 0);
                        button.Tag = messageOption;
                        button.Click += new RoutedEventHandler(button_Click);
                        this.ButtonPanel.Children.Add(button);
                    }
                    break;
            }
        }

        #endregion
        #region ShowAsync

        public async Task ShowAsync(MessageContext message, object owner)
        {
            this.Title = message.Title;
            InitializeContext(message);
            await ShowAsync();
        }

        #endregion

        #endregion
        #region Private Members

        private MessageContext Message { get; set; }

        #endregion
        #region Events

        #region button_Click

        private void button_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            var option = button.Tag as MessageOption;
            this.Message.Answers.Add(option);
            Hide();
        }

        #endregion
        #region ContentDialog_PrimaryButtonClick

        private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var option = this.Message.MessageOptions[0];
            this.Message.Answers.Add(option);
            Hide();
        }

        #endregion
        #region ContentDialog_SecondaryButtonClick

        private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var option = this.Message.MessageOptions[1];
            this.Message.Answers.Add(option);
            Hide();
        }

        #endregion

        #endregion
        #region Constructors

        public MessageDialog()
        {
            this.InitializeComponent();
        }

        #endregion
    }
}
